import React from 'react';
import ReactDOM from 'react-dom/client';
import { App, EduApp } from './App';

import { persist, useStageStore } from 'codeck';
import './tailwind.css';
import '@arco-design/web-react/dist/css/arco.css';
import './index.css';
import './reg';
import { openWelcomeModal } from './components/modal/RunCode';
import { Notification } from '@arco-design/web-react';



let url = '';
let reg = new RegExp("(^|&)id=([^&]*)(&|$)");
let r = window.location.search.substr(1)
  .match(reg);

if (r != null) {
  url = decodeURI(r[2]);
  openFileURL(url)
  useStageStore.getState().focus();
  Notification.success({
    style: { width: 500, marginTop: 45 },
    title: '节点数据导入',
    content:
      '已导入外部节点数据',
  })
} else {
  try {
    persist.loadFromLocalStorage();
    useStageStore.getState().focus();
    Notification.success({
      style: { width: 500, marginTop: 45 },
      title: '节点数据导入',
      content:
        '已导入历史节点数据',
    })
  } catch (err) {
    console.warn(err);
  }
}
async function openFileURL(url: string) {
  let libSource = await (await fetch('https://static.codemao.cn/pickduck/' + url + '.noda')).text();
  persist.loadUrl(libSource);
}


reg = new RegExp("(^|&)box=([^&]*)(&|$)");
r = window.location.search.substr(1)
  .match(reg);
if (r != null) {
  ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
    <EduApp />
  );

} else {
  ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
    <App />
  );
}
